package com.sitc.administrator.application.pushMsg;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.igexin.sdk.PushManager;

import java.io.File;


public class GetuiSdk{

    private static final String TAG = "GetuiSdk";

    private Activity activity;
    private Context context;
    // SDK服务是否启动.
    private boolean isServiceRunning = true;
    private String appkey = "";
    private String appsecret = "";
    private String appid = "";

    private static final int REQUEST_PERMISSION = 0;

    public  GetuiSdk(Context context) {

		Log.d(TAG, "initializing sdk...");
		//this.activity = activity;
		//this.context = activity.getApplicationContext();
        this.context = context;

        isServiceRunning = true;
        parseManifests();
		Log.d(TAG, "appkey：" + appkey );
		Log.d(TAG, "appsecret：" + appsecret );
		Log.d(TAG, "appid：" + appid );

        PackageManager pkgManager = context.getPackageManager();

        // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
        boolean sdCardWritePermission =
                pkgManager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,context.getPackageName()) == PackageManager.PERMISSION_GRANTED;

        // read phone state用于获取 imei 设备信息
        boolean phoneSatePermission =
                pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE,context.getPackageName()) == PackageManager.PERMISSION_GRANTED;


//        if (Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission) {
//            requestPermission();
//        } else {
//            PushManager.getInstance().initialize(context.getApplicationContext(), UserPushService.class);
//        }

        PushManager.getInstance().initialize(context.getApplicationContext(), UserPushService.class);

        // 注册 intentService 后 PushDemoReceiver 无效, sdk 会使用 DemoIntentService 传递数据,
        // AndroidManifest 对应保留一个即可(如果注册 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注册了
        // IntentService, 必须在 AndroidManifest 中声明)
        PushManager.getInstance().registerPushIntentService(context.getApplicationContext(), UserIntentService.class);


        // cpu 架构
        Log.d(TAG, "cpu arch = " + (Build.VERSION.SDK_INT < 21 ? Build.CPU_ABI : Build.SUPPORTED_ABIS[0]));

        // 检查 so 是否存在
        File file = new File(context.getApplicationInfo().nativeLibraryDir + File.separator + "libgetuiext3.so");
        Log.e(TAG, "libgetuiext3.so exist = " + file.exists());
    }



    private void requestPermission() {
//        ActivityCompat.requestPermissions(activity,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE},
//                REQUEST_PERMISSION);
    }

    private void parseManifests() {
        String packageName = context.getApplicationContext().getPackageName();
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            if (appInfo.metaData != null) {
                appid = appInfo.metaData.getString("PUSH_APPID");
                appsecret = appInfo.metaData.getString("PUSH_APPSECRET");
                appkey = appInfo.metaData.getString("PUSH_APPKEY");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
